package com.fancyy.scm.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.fancyy.scm.R;
import com.fancyy.scm.base.BaseActivity;
import com.fancyy.scm.base.adapter.BRBaseAdapter;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.base.annotation.Translucent;
import com.fancyy.scm.base.http.BRDataListener;
import com.fancyy.scm.base.http.BRDataTransport;
import com.fancyy.scm.base.http.DataTransport;
import com.fancyy.scm.bean.HttpMessage;
import com.fancyy.scm.bean.ProgressTracking;
import com.fancyy.scm.bean.ScanResult;
import com.fancyy.scm.databinding.DishiyeBinding;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

import static com.fancyy.scm.BRConstant.SCANNING;
import static com.fancyy.scm.BRConstant.SCANNING_TRACKING;

@Translucent
@Layout(R.layout.dishiye)
public class TrackingActivity extends BaseActivity<DishiyeBinding> {

        public String type; // Process  QC

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        type = getIntent().getStringExtra("type");
        setNeedScan(true);
        super.onCreate(savedInstanceState);
        //if (type=="QC")
        getDataBinding().ProcessTitle.setText(type+" Tracking");
    }

    public void onScanningClick(View view) {
        if(!getPDA()) {
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            String code = data.getStringExtra(CodeUtils.RESULT_STRING);
            handleCode(code);
        } else if (requestCode == 2 && resultCode == RESULT_OK) {
            onResetClick(null);
        }
    }

    private void handleCode(String code) {
        getDataBinding().scan.setText(code);
        //BRDataTransport.createGet(SCANNING_TRACKING + "?barCode=" + getDataBinding().scan.getText().toString())
        BRDataTransport.create(SCANNING_TRACKING)
                .addParam("barCode",getDataBinding().scan.getText().toString())
                .addParam("ScanType", type)
                .addParam("Operator", getBRApplication().getUserInfo().userAccount)
                .addProgressing(this, "Progressing...")
                .execute(new BRDataListener<ProgressTracking>() {
                    @Override
                    public void onSuccess(ProgressTracking data) {
                        getDataBinding().model.setText(data.model);
                        getDataBinding().packageQty.setText(data.packageQty);
                        getDataBinding().currentQty.setText(data.currentQty);
                        playSound(R.raw.success);
                        onSubmitClick(null);
                    }

                    @Override
                    public void onFail(int status, String failMessage) {
                        super.onFail(status, failMessage);
                        playSound(R.raw.error);
                    }
                }, ProgressTracking.class);
    }

    @Override
    public void doScan(String data) {
        handleCode(data);
//        closeScan();
    }

    public void onResetClick(View view) {
        getDataBinding().scan.setText(null);
        getDataBinding().model.setText(null);
        getDataBinding().packageQty.setText(null);
        getDataBinding().currentQty.setText(null);
    }

    public void onSubmitClick(View view) {
        if (!getDataBinding().model.getText().equals("")) {
            Intent intent = new Intent(this, TrackingBActivity.class);
            intent.putExtra("barCode", getDataBinding().scan.getText().toString());
            intent.putExtra("model", getDataBinding().model.getText().toString());
            intent.putExtra("packageQty", getDataBinding().packageQty.getText().toString());
            intent.putExtra("currentQty", getDataBinding().currentQty.getText().toString());
            intent.putExtra("type", type);
            startActivityForResult(intent, 2);
        }
    }
}
